package com.scau.controller;

import com.scau.entity.User;
import com.scau.entity.UserVO;
import com.scau.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController //需要这个注解
@RequestMapping("/user")
public class UserController {

    @Value("${server.port}")
    private String port;
    @Value("${web.upload-path}")
    private String fileprefix;

    @GetMapping("/port")
    public String port(){
        return this.port;
    }

    @Autowired
    private UserRepository userRepository;


    @GetMapping("/findAll/{index}/{limit}")
    public UserVO findAll(@PathVariable("index") int index, @PathVariable("limit") int limit){
        System.out.println(index + " " +  limit);
        List<User> list = userRepository.findAll(index, limit);
        return new UserVO(0,"",userRepository.count(),list);
    }

    @GetMapping("/login/{id}/{password}/{type}")
    public User login(@PathVariable("id") int id, @PathVariable("password") String password,@PathVariable("type") int type){
        return userRepository.login(id,password,type);
    }

    @PostMapping(value = "/save")
    public void saveUser(@RequestBody User user){
        userRepository.save(user);
    }

    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") int id){
        return userRepository.findById(id);
    }

    @PostMapping(value = "/update")
    public void update(@RequestBody User user){
        userRepository.update(user);
    }

    @GetMapping("/changeLogoUrl")
    public void changeLogoUrl(@RequestBody User user){
        userRepository.changeLogoRul(user);
    }

}
